{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 1 基础 课后练习及补充" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.1 数据类型相关练习" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.1.1 数据类型转换\n", "运行下面单元格,了解`int()`,`float()`,`str()`,`type()`等的用法" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# str -> int\n", "X = int('ABCD', 16)\n", "print(X)\n", "type(X)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# int -> float\n", "a = 520\n", "b = float(a)\n", "b" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# float -> str\n", "a = 5.99\n", "b = str(a)\n", "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "接下来,请尝试将字符串`'520'`转化为小数,可以使用`float()`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = '520'\n", "b = " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "运行下面单元格,了解字符串的切片操作" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a_string = 'Hello' + ' ' + \"Women Who Code!\"\n", "print(a_string)\n", "print(\"str[0] :\" + a_string[0])\n", "print(\"str[2:5]:\" + a_string[2:5]) # Python speak: slicing\n", "print(\"str[2:] :\" + a_string[2:])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.2 列表操作" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "列表是个框,什么都可以往里装" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lis = [ \"WWCode\", 786 , 2.23, 'singapore', 70.2 ]\n", "print(lis[0:3])\n", "type(lis)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "列表的索引,先猜猜下面这个单元格能得到什么,再运行" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lis[0][2:]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "如果我想从`lis`里得到`'sing'`应该怎么做呢" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "列表的元素是可以修改的" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lis[2] = 3.3\n", "lis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "请尝试将列表最后一个元素更改为50" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "列表有一个非常好用的操作,叫list comprehension,可以运行下面单元格感受一下" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "symbols = '$¢£¥€¤'\n", "codes = [ord(symbol) for symbol in symbols]\n", "codes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`ord()`的作用是返回符号的unicode编码" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.3 元组操作" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t1 = ( \"WWCode\", 100000 , 0.5 ) # org name, members, proportion of engineers\n", "t2 = 'Singapore', 1160.5\n", "t_singleton = ('We',) # singleton\n", "t_empty = ()\n", "print(type(t1)); print(type(t2))\n", "print(t_singleton);\n", "type(t_empty)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "元组元素不可修改,尝试运行下列代码" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "t1[2] = 20" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.4 字典操作" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dict1 = {'name':'Women Who Code Singapore', \n", " 'org':'WWCode', \n", " 'city':'Singapore',\n", " 'members':1260}\n", "print(dict1['org'])\n", "type(dict1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "字典元素可以更改" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 增加元素\n", "dict1['rank'] = 10" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 获取元素\n", "dict1['city']" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 获取不知是否存在的元素\n", "dict1.get('org','不存在')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dict1.get('ord','不存在')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.5 Sets 集合 \n", "* Sets are a methematical concept, they are a lot like dictionaries with keys but no corresponding values. \n", "* 跟数学的概念很相似,类似于字典的键,但没有对应的值\n", "* Sets are enclosed by curly braces, elements seperated by comma, '{','}'. \n", "* 用花括弧\n", "* Sets do not support indexing or slicing, and do not have inherent order. \n", "* 不支持下标应用和切片" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "wwcode_asia_networks = {'Bangalore','Beijing','Chennai','Delhi','Gujarat','Hong Kong','Kuala Lumpur','Manila','Pune','Rajasthan','Shanghai','Singapore','Taipei','Tel-Aviv','Tokyo'}\n", "type(wwcode_asia_networks)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(wwcode_asia_networks)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "wwcode_asia_networks[1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.6 运算和布尔运算" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 1 + 2 # Addition\n", "y = 3 - 4 # Subtraction\n", "z = 5 * 6 # Multiplication\n", "a = z / y # Division\n", "b = z % x # Modulus\n", "c = y ** x # Exponent\n", "d = c // x # Floor Division\n", "print(\"x:\" + str(x) + \" y:\" + str(y) + \" z:\" + str(z) + \n", " \" a:\" + str(a) + \" b:\" + str(b) + \" c:\" + str(c) + \" d:\" + str(d))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "运算符|\t描述|\t实例\n", "-| :-: | -\n", "=\t|简单的赋值运算符\t|c = a + b 将 a + b 的运算结果赋值为 c\n", "+=\t|加法赋值运算符\t|c += a 等效于 c = c + a\n", "-=\t|减法赋值运算符\t|c -= a 等效于 c = c - a\n", "*=\t|乘法赋值运算符\t|c *= a 等效于 c = c * a\n", "/=\t|除法赋值运算符\t|c /= a 等效于 c = c / a\n", "%=\t|取模赋值运算符\t|c %= a 等效于 c = c % a\n", "**=\t|幂赋值运算符\t|c \\*\\*= a 等效于 c = c \\*\\* a\n", "//=\t|取整除赋值运算符\t|c //= a 等效于 c = c // a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "布尔运算和比较运算\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(a == b) # equals\n", "print(a != b) # not equals\n", "print(a > b) # greater than\n", "print(a < b) # lesser than\n", "print(a >= b) # greater than or equal\n", "print(a <= b) # lesser than or equal" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "Logical Operators 逻辑运算符 \n", "and or not" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "not True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "not 0" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "not 4" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "3 < 4 < 5" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "3 < 4 < 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "3 < 4 and 4 < 5" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "3 < 5 or 8 > 5" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a_string = \"Women Who Code\"\n", "print(\"Women\" in a_string)\n", "print(\"Men\" not in a_string)\n", "print(len(\"Women Who Code\") is len(a_string))\n", "print(len(\"Hello World!\") is not len(a_string))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 判断闰年怎么判断呢\n", "year = 2100 # eval(input('输入年份(四位数)'))\n", "# 大家来试试\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "is 和 is not 运算符 与==以及!=的区别" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "is 用于判断两个变量引用对象是否为同一个, == 用于判断引用变量的值是否相等。" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 5\n", "y = 5\n", "print(x == y)\n", "print(x is y)\n", "print(id(x))\n", "print(id(y))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "help(id)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "id(1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "id(\"abc\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "id([1, 2, 3])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = \"abcabcabcabcabcabcabcabcabcabc\"\n", "y = \"abcabcabcabcabcabcabcabcabcabc\"\n", "print(x == y)\n", "print(x is y)\n", "print(id(x))\n", "print(id(y))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 数组比较\n", "x = [1, 2, 3]\n", "y = [1, 2, 3]\n", "print(x == y)\n", "print(x is y)\n", "print(id(x))\n", "print(id(y))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 元组比较\n", "x = (1, 2, 3)\n", "y = (1, 2, 3)\n", "print(x == y)\n", "print(x is y)\n", "print(id(x))\n", "print(id(y))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 字典比较\n", "x = {\"id\": 1, \"name\": \"Tom\", \"age\": 18}\n", "y = {\"id\": 1, \"name\": \"Tom\", \"age\": 18}\n", "print(x == y)\n", "print(x is y)\n", "print(id(x))\n", "print(id(y))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 集合比较\n", "x = set([1, 2, 3])\n", "y = set([1, 2, 3])\n", "print(x == y)\n", "print(x is y)\n", "print(id(x))\n", "print(id(y))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 赋值后比较\n", "x = [1, 2, 3]\n", "y = x\n", "print(x == y)\n", "print(x is y)\n", "print(id(x))\n", "print(id(y))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "空值比较" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "none_type = None\n", "none_type is None" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1.A 附录 一些可能会用到的知识\n", "这部分内容在使用python的过程中可能会用得上,可以逐个运行单元格感受一下~" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.A.1 Reserve Words保留字\n", "The following identifiers are used as reserved words of the language, and cannot be used as ordinary identifiers.\n", "一些关键字是系统自带的保留字,即不能用作为标识符" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import keyword\n", "keyword.kwlist" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 1.A.2 build-in functions内置函数\n", "这些函数不能作为变量名,可以作为函数直接调用,如:`print()` `input()` \n", "`dir(\\__builtins__)`可以看到内置函数列表" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dir(__builtins__)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "aa = 3\n", "aaaa = 5\n", "dir() #python内置函数;不带参数时,返回当前范围内的变量、方法和定义的类型列表\n", " #带参数是,返回参数的属性、方法列表" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "help(dir)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "help(print) # 内置函数,查看函数或模块用途的详细说明" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hit the **STOP** square button in the button ribbons/bar on top to continue to next cell." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#help(str)\n", "import copy\n", "help(copy.copy)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "本篇整理:肖桐" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.3" } }, "nbformat": 4, "nbformat_minor": 4 }